package br.roove.bomotivo.ui;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import br.roove.bomotivo.model.Imagem;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.twotoasters.android.horizontalimagescroller.image.ImageToLoad;
import com.twotoasters.android.horizontalimagescroller.image.ImageToLoadUrl;
import com.twotoasters.android.horizontalimagescroller.widget.HorizontalImageScroller;
import com.twotoasters.android.horizontalimagescroller.widget.HorizontalImageScrollerAdapter;
import com.twotoasters.android.horizontalimagescroller.widget.SelectionToggleOnItemClickListener;

public class HorizontalScrollLayout extends Activity {
	
	ProgressDialog pd;
	ArrayList<String> eventImages = new ArrayList<String>();
	ArrayList<Imagem> allImages = new ArrayList<Imagem>();
	ArrayList<ImageToLoad> imagesToLoad = new ArrayList<ImageToLoad>();
	String URL = "http://bomotivo.roove.com.br/service/imagens/";
	String BASE_URL = "http://bomotivo.roove.com.br";
	String nameHeader;
	String valueHeader;
	int currentItem;
	int id;
	Bundle b;
	private String nome_balada;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_horizontal_scroll_layout);
		
		b = getIntent().getExtras();
		nome_balada = b.getString("nome");
		id = b.getInt("id");
		
		AsyncHttpClient client = new AsyncHttpClient();
		client.get(URL + id + ".json", null, new JsonHttpResponseHandler(){
			
			

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				pd = ProgressDialog.show(getApplicationContext(), null, "Carregando...");
			}

			@Override
			public void onSuccess(JSONArray arg0) {
				// TODO Auto-generated method stub
				super.onSuccess(arg0);
				pd.dismiss();

		try {
			JSONArray jsonArray = arg0;
			for (int i = 0; i< jsonArray.length(); i++){
				Imagem img = new Imagem();
				JSONObject jsonObj = jsonArray.getJSONObject(i);
				img.setUrl_foto_aumentada(BASE_URL + jsonObj.getString("original"));
				String imageToLoad;
				if (jsonObj.getString("reduzido").equals("")){
					img.setUrl_foto_menor(BASE_URL + jsonObj.getString("original"));
					imageToLoad = BASE_URL + jsonObj.getString("original");
				}else{
					img.setUrl_foto_menor(BASE_URL + jsonObj.getString("reduzido"));
					imageToLoad = BASE_URL + jsonObj.getString("reduzido");
				}
				img.setUrl_foto_menor(BASE_URL + jsonObj.getString("original"));
				allImages.add(img);
				eventImages.add(imageToLoad);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		for (int i = 0; i < eventImages.size(); i++){
			ImageToLoadUrl imageToLoad = new ImageToLoadUrl(eventImages.get(i));
			imageToLoad.setCanCacheFile(true);
			imagesToLoad.add(imageToLoad);
		}
		
		
		
		//View v = inflater.inflate(R.layout.fotos_layout, container, false);
		//TextView textTab = (TextView)v2.findViewById(R.id.textViewTab);
		//textTab.setText("GALERIA - " + "");
	
			}
			
		});
		HorizontalImageScroller horizontalIScroller = (HorizontalImageScroller)findViewById(R.id.horizontal);
		horizontalIScroller.setAdapter(new HorizontalImageScrollerAdapter(getApplicationContext(), imagesToLoad));
		currentItem = horizontalIScroller.getCurrentImageIndex();
		horizontalIScroller.setOnItemClickListener(new SelectionToggleOnItemClickListener() {	

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				
				ImageView imgSelected = (ImageView)findViewById(R.id.imagem_aumentada_activity);
				UrlImageViewHelper urlHelper = new UrlImageViewHelper();
				urlHelper.setUrlDrawable(imgSelected, allImages.get(currentItem).getUrl_foto_aumentada());
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.horizontal_scroll_layout, menu);
		return true;
	}

}
